package com.zheng.oss.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.zheng.common.annotation.BaseService;

import com.zheng.oss.common.constant.enums.FilePermission;
import com.zheng.oss.dao.model.OssFile;
import com.zheng.oss.dao.model.OssRoleFile;
import com.zheng.oss.dao.mapper.OssRoleFileMapper;
import com.zheng.oss.rpc.api.OssFileService;
import com.zheng.oss.rpc.api.OssRoleFileService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.upms.dao.model.UpmsRole;
import com.zheng.upms.rpc.api.UpmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-05-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class OssRoleFileServiceImpl extends BaseServiceImpl<OssRoleFileMapper, OssRoleFile> implements OssRoleFileService {
    @Autowired
    private OssRoleFileMapper roleFileMapper;
    @Autowired
    private UpmsRoleService roleService;
    @Autowired
    private OssFileService fileService;
    private final int adminRoleId = 1;

    public List<Map<String, Object>> selectRoleFile(Integer fileId, Integer roleId) {
        return roleFileMapper.selectRoleFile(fileId, roleId);
    }

    /**
     * 根据父文件的权限来设置子文件的权限
     *
     * @param file 当前文件新增的
     * @return
     */
    public boolean insertRoleFile(OssFile file) {
        EntityWrapper<OssRoleFile> roleFileEntityWrapper = new EntityWrapper<>();
        roleFileEntityWrapper.eq("file_id", file.getPid());
        List<OssRoleFile> roleFiles = selectList(roleFileEntityWrapper);
        List<UpmsRole> roles = roleService.selectList(null);
        //根据角色来添加
        for (UpmsRole role : roles) {
            boolean flag = true;
            for (OssRoleFile roleFile : roleFiles) {
                if (roleFile.getRoleId().equals(role.getRoleId())) {
                    roleFile.setFileId(file.getId());
                    flag = false;
                    break;
                }
            }
            if (flag) {
                //如果没有，就给与默认权限
                OssRoleFile roleFile = new OssRoleFile();
                roleFile.setPermission(FilePermission.VIEW.getKey());
                roleFile.setRoleId(role.getRoleId());
                roleFile.setFileId(file.getId());
                roleFiles.add(roleFile);
            }
        }
        return insertBatch(roleFiles);
    }

    public boolean filePermission(List<OssRoleFile> roleFiles, Boolean effect) {
        Integer fileId = roleFiles.get(0).getFileId();
        //查看是否有超级管理员的权限，没有就增加
        int count = selectCount(new EntityWrapper<OssRoleFile>().eq("role_id", adminRoleId).eq("file_id", fileId));
        if (count < 1) {
            OssRoleFile adminRoleFile = new OssRoleFile();
            adminRoleFile.setId(null);
            adminRoleFile.setRoleId(adminRoleId);
            adminRoleFile.setFileId(fileId);
            adminRoleFile.setPermission(FilePermission.ALL.getKey());
            roleFiles.add(adminRoleFile);
        }
        //如果影响子节点，就修改子孙权限信息
        if (effect) {
            EntityWrapper<OssFile> fileEntityWrapper;
            EntityWrapper<OssRoleFile> roleFileEntityWrapper;
            List<OssRoleFile> childrenRoleFiles = new ArrayList<>();
            for (OssRoleFile roleFile : roleFiles) {
                fileEntityWrapper = new EntityWrapper<>();
                fileEntityWrapper.like("path", "%," + roleFile.getFileId() + ",%");
                //查找所有子孙文件
                List<OssFile> childrenFiles = fileService.selectList(fileEntityWrapper);
                //获取所有子孙文件的id
                List<Integer> childrenFileIds = new ArrayList<>();
                childrenFiles.forEach(childrenFile -> {
                    childrenFileIds.add(childrenFile.getId());
                });
                roleFileEntityWrapper = new EntityWrapper<>();
                roleFileEntityWrapper.in("file_id", childrenFileIds).eq("role_id", roleFile.getRoleId());
                //删除子孙文件的权限信息
                delete(roleFileEntityWrapper);
                //遍历子文件，依次添加新的权限信息
                for (OssFile childFile : childrenFiles) {
                    OssRoleFile childRoleFile = new OssRoleFile();
                    childRoleFile.setFileId(childFile.getId());
                    childRoleFile.setRoleId(roleFile.getRoleId());
                    childRoleFile.setPermission(roleFile.getPermission());
                    childrenRoleFiles.add(childRoleFile);
                }
            }
            System.out.println(childrenRoleFiles);
            roleFiles.addAll(childrenRoleFiles);
        }
        //更新权限
        return insertOrUpdateBatch(roleFiles);
    }

}
